local StoryBehavior = require("gameLogic.story.behavior.StoryBehavior")
local StoryAction = class("StoryAction", StoryBehavior)
local StoryConst = require("const.StoryConst")
local AnimConst = require("const.AnimConst")
local GameUtils = require("utils.GameUtils")
local Vector3 = require("utils.Vector3")

function StoryAction:ctor()
    StoryAction.super.ctor(self)
end

function StoryAction:_active()
    if self._init then
        return
    end
    self._init = true
    local params = self:getParams()
    local action = params.valueStr
    local actionFunc = self["_do_" .. action .. "_Action"]
    if actionFunc and type(actionFunc) == "function" then
        actionFunc(self)
    else
        self:setDone(true)
    end
end

function StoryAction:_do_run_Action()
    local params = self:getParams()
    local model = self:getActuator():findHero(params.heroName)
    local modelTrans = model.transform
    local animator = model:GetComponent(typeof(CS.UnityEngine.Animator))
    animator:SetBool(AnimConst.Moving, true)
    modelTrans:LookAt(params.pos)
    local moveTo = modelTrans:DOMove(params.pos, params.duration)
    local sequence = DOTween.Sequence()
    sequence:Append(moveTo)
    sequence:AppendCallback(
        function()
            animator:SetBool(AnimConst.Moving, false)
            self:setDone(true)
        end
    )
end

function StoryAction:_do_attack_Action()
    local params = self:getParams()
    local model = self:getActuator():findHero(params.heroName)
    local modelTrans = model.transform
    local animator = model:GetComponent(typeof(CS.UnityEngine.Animator))
    animator:SetTrigger(AnimConst.Attack)

    local time = GameUtils.getAnimationLength(animator, AnimConst.CLIP_ATTACK)
    local sequence = DOTween.Sequence()
    sequence:AppendInterval(time)
    sequence:AppendCallback(
        function()
            self:setDone(true)
        end
    )
end

function StoryAction:_do_hero_effect_Action()
    local params = self:getParams()
    local effectName = params.res
    local object = G_ResourceManager:loadEffect(effectName)
    local gameObject = GameUtils.instantiate(object)
    gameObject.transform.position = params.pos
    gameObject.transform.eulerAngles = params.eulerAngles
    gameObject.transform.localScale = Vector3.new(5, 5, 5)
    local sequence = DOTween.Sequence()
    sequence:AppendInterval(params.duration)
    sequence:AppendCallback(
        function()
            GameUtils.destroy(gameObject)
            self:setDone(true)
        end
    )
end

function StoryAction:_doOtherAction()
end

return StoryAction
